package com.tuanzi.loan.business.service.xhh.settlementsystem.impl;

import com.tuanzi.loan.business.entity.family.ProductInfo;
import com.tuanzi.loan.business.entity.xhh.settlementsystem.XhhSettlementProduct;
import com.tuanzi.loan.business.repository.xhh.settlementsystem.XhhSettlementProductRepository;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementIncomeActivityService;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementIncomeService;
import com.tuanzi.loan.business.service.xhh.settlementsystem.XhhSettlementProductService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.repository.JPAAuditRepository;
import com.tuanzi.loan.core.service.BaseAuditServiceImpl;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.xhh.settlementsystem.XhhSettlementProductRequest;
import com.tuanzi.loan.vo.response.xhh.settlementsystem.XhhSettlementProductResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author caiwanlin
 * @create 2018-08-16 20:03
 */
@Debug
@Service
@Transactional
@Retryable(value = {RetryException.class})
public class XhhSettlementProductServiceImpl extends BaseAuditServiceImpl<XhhSettlementProduct, Long> implements XhhSettlementProductService {

    @Autowired
    private XhhSettlementIncomeActivityService xhhSettlementIncomeActivityService;
    @Autowired
    private XhhSettlementIncomeService xhhSettlementIncomeService;
    @Autowired
    private XhhSettlementProductRepository xhhSettlementProductRepository;

    @Override
    public Page<XhhSettlementProductResponse> list(PageSearcher<XhhSettlementProductRequest> pageSearcher) {
        return xhhSettlementProductRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
    }

    @Override
    public List<ProductInfo> getActivityList() {
        return xhhSettlementProductRepository.getActivityList();
    }

    @Override
    public List<XhhSettlementProduct> getProductList() {
        return xhhSettlementProductRepository.getProductList();
    }

    @Override
    public XhhSettlementProduct findByProductName(String productName) {
        return xhhSettlementProductRepository.findByProductName(productName);
    }

    @Override
    public void deleteByCompanyId(Long companyId) {
        xhhSettlementProductRepository.deleteByCompanyId(companyId);
    }

    @Override
    public void onDelete(Long id) {
        xhhSettlementIncomeActivityService.deleteByProductId(id);
        xhhSettlementIncomeService.deleteByProductId(id);
        xhhSettlementProductRepository.delete(id);
    }

    @Override
    protected JPAAuditRepository<XhhSettlementProduct, Long> getRepository() {
        return xhhSettlementProductRepository;
    }
}
